package com.zhhh.controller;

import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import com.zhhh.authorization.annotation.Authorization;
import com.zhhh.authorization.annotation.CurrentUser;
import com.zhhh.authorization.manager.TokenManager;
import com.zhhh.authorization.model.TokenModel;
import com.zhhh.config.ResultStatus;
import com.zhhh.domain.BUser;
import com.zhhh.domain.User;
import com.zhhh.model.ResultModel;
import com.zhhh.repository.BUserRepository;
import com.zhhh.repository.UserRepository;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 获取和删除token的请求地址，在Restful设计中其实就对应着登录和退出登录的资源映射
 */
@RestController
@RequestMapping("/tokens")
public class TokenController {

	@Autowired
	private UserRepository userRepository;

	@Autowired
	private TokenManager tokenManager;
	
	@Autowired
	private BUserRepository uRepository;

	@RequestMapping(value = "/login",method = RequestMethod.POST)
	@ApiOperation(value = "登录")
	public ResponseEntity<ResultModel> login(@RequestParam String username, @RequestParam String password) {
		Assert.notNull(username, "username can not be empty");
		Assert.notNull(password, "password can not be empty");
		//User user = userRepository.findByUsername(username);
		BUser user = uRepository.findByUsername(username);
		if (user == null || // 未注册
				!user.getPassword().equals(password)) { // 密码错误
			// 提示用户名或密码错误
			return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR),
					HttpStatus.NOT_FOUND);
		}
		// 生成一个token，保存用户登录状态
		TokenModel model = tokenManager.createToken(user.getId());
		return new ResponseEntity<>(ResultModel.ok(model), HttpStatus.OK);
	}

	@RequestMapping(method = RequestMethod.DELETE)
	@Authorization
	@ApiOperation(value = "退出登录")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"), })
	public ResponseEntity<ResultModel> logout(@CurrentUser User user) {
		tokenManager.deleteToken(user.getId());
		return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK);
	}
	
	@Authorization
	@RequestMapping(value = "/queryUser",method = RequestMethod.POST)
	@ApiOperation(value = "根据用户名查询用户")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"), })
	public ResponseEntity<ResultModel> queryUser(@RequestParam String username) {

		User user = userRepository.findByUsername(username);
		return new ResponseEntity<>(ResultModel.ok(user), HttpStatus.OK);
	}
	
	@Authorization
	@RequestMapping(value = "/queryUserAll",method = RequestMethod.POST)
	@ApiOperation(value = "查询所有用户")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"), })
	public ResponseEntity<ResultModel> queryUserAll() {

		List<User> findAll = (List<User>) userRepository.findAll();
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}
	
	@RequestMapping(value = "/addUser",method = RequestMethod.POST)
	@ApiOperation(value = "用户注册")
	public ResponseEntity<ResultModel> addUser(@RequestParam String username, @RequestParam String password,@RequestParam String nickname) {
		
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setNickname(nickname);
		userRepository.save(user);

		List<User> findAll = (List<User>) userRepository.findAll();
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}
	
	@Authorization
	@RequestMapping(value = "/updatePassWord",method = RequestMethod.POST)
	@ApiOperation(value = "修改密码")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"), })
	public ResponseEntity<ResultModel> updatePassWord(@RequestParam String username, @RequestParam String password) {
		User user = userRepository.findByUsername(username);
		user.setPassword(password);
		userRepository.save(user);
		List<User> findAll = (List<User>) userRepository.findAll();
		
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}
	
	
	@Authorization
	@RequestMapping(value = "/deleteUser",method = RequestMethod.POST)
	@ApiOperation(value = "删除用户")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"), })
	public ResponseEntity<ResultModel> deleteUser(@RequestParam Long id) {
		userRepository.deleteById(id);
		List<User> findAll = (List<User>) userRepository.findAll();
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}
	
	
	
	
	

}
